var app = angular.module('interApp', []);

var keyTip="请输入待检索的关键字：接口名称、项目名称";
var ipTip="请输入待检索的IP";
var portTip="请输入待检索的端口号";

app.controller('interController', function($scope, $http, InterService) {
	$("#interfaceType").select2({
		 placeholder: "请选择接口类型",
		 allowClear: true
	}); 
	
	$("#enableStatus").select2({
		 placeholder: "请选择接口部署状态",
		 allowClear: true
	});
	
	$("#runStatus").select2({
		 placeholder: "请选择接口运行状态",
		 allowClear: true
	});
	
	$http.get('qz/channel/getAll?status=2').success(function(response){
		$scope.channelList=response.data;
		$("#channelId").select2({
			 placeholder: "请选择机构",
			 allowClear: true
		});
		$("#channelId").change(function(){  
			$("#businessId").select2("val", "");
			var channelId=$("#channelId").val();
			var url=channelId.length>0?'qz/business/getByChannelId/'+channelId:'qz/business/getAll/';
			$http.get(url).success(function(response){
				$scope.businessList=response.data;
			});
		});
    });
		
	$http.get('qz/business/getAll').success(function(response){
		$scope.businessList=response.data;
		$("#businessId").select2({
			 placeholder: "请选择业务分类",
			 allowClear: true
		});
	});
		
	$http.get('sys/dic/getByGroup/jym').success(function(response){
		$scope.channelTypeList=response.data;
		$("#channelType").select2({
			 placeholder: "请选择业务细类",
			 allowClear: true
		});
	});
		
	$http.get('sys/dic/getByGroup/txxy').success(function(response){
		$scope.protocolList=response.data;
		$("#protocol").select2({
			 placeholder: "请选择通信协议",
			 allowClear: true
		}); 
	});
		
	$http.get('sys/dic/getByGroup/bwgs').success(function(response){
		$scope.packetFormatList=response.data;
		$("#packetFormat").select2({
			 placeholder: "请选择报文格式",
			 allowClear: true
		});
	});
		
	$http.get('sys/dic/getByGroup/jhfs').success(function(response){
		$scope.idnteractiveList=response.data;
		$("#idnteractive").select2({
			 placeholder: "请选择交互方式",
			 allowClear: true
		});
    });
	
	$http.get('sys/dic/getByGroup/yhqd').success(function(response){
		$scope.zcyhList=response.data;
		$("#zcyh").select2({
			 placeholder: "请选择支持银行",
			 allowClear: true
		});
    });
	
	$http.get('sys/dic/getByGroup/zfklx').success(function(response){
		$scope.zfklxList=response.data;
		$("#zfklx").select2({
			 placeholder: "请选择支持的支付卡类型",
			 allowClear: true
		});
    });
	
	$http.get('sys/dic/getByGroup/zfksx').success(function(response){
		$scope.zfksxList=response.data;
		$("#zfksx").select2({
			 placeholder: "请选择支持的支付卡属性",
			 allowClear: true
		});
    });
	
	$scope.load=function(isRest){
		$scope.sel=$scope.sel?$scope.sel:{};
		var pageNum=$scope.pageInfo?isRest==false?$scope.pageInfo.pageNum:1:1;
		var pageSize=$scope.pageInfo?$scope.pageInfo.pageSize:10;
		$scope.sel.pageNum=pageNum;
		$scope.sel.pageSize=pageSize;
		
		var param={};  
        for(var k in $scope.sel){ 
        	param[k] = $scope.sel[k];  
        	if(k=="key"&&$scope.sel[k]==keyTip){
        		delete param[k];
        	}else if(k=="ip"&&$scope.sel[k]==ipTip){
        		delete param[k];
        	}else if(k=="port"&&$scope.sel[k]==portTip){
        		delete param[k];
        	}
        } 
		var interfaceType=$("#interfaceType").val();
		var enableStatus=$("#enableStatus").val();
		var runStatus=$("#runStatus").val();
		
		if(interfaceType!=''){
			param.interfaceType=parseInt(interfaceType)
		}
		if(enableStatus!=''){
			param.enableStatus=parseInt(enableStatus)
		}
		if(runStatus!=''){
			param.runStatus=parseInt(runStatus)
		}
		InterService.list(param).success(function (response) {
			$scope.pageInfo=response.data;
			$scope.pageInfo.pageLinks=getPageLinks(response.data.pageNum,response.data.pages);
	    });
	}
	
	$scope.load();
    $scope=pagination.init($scope);
    $scope.interCreator=function(){
    	redirectUrl("manage/inter/","add.jsp");
    }
    $scope.refreshData=function(){
    	$scope.load();
    }
    $scope.delTip=function(id,mes){
    	$scope.delItem={id:id,mes:mes};
    	$('#box-del').modal({
    		show: true
    	});
    	
    }
    $scope.del=function(id){
    	InterService.del(id).success(function (response) {
    		$('#box-del').modal('hide');
    		$scope.load();
    	});
    }
    $scope.resetSel=function(){
    	$scope.sel={};
    	$("#interfaceType").select2("val", "");
    	$("#channelId").select2("val", "");
    	$("#businessId").select2("val", "");
		$("#channelType").select2("val", "");
		$("#enableStatus").select2("val", "");
		$("#runStatus").select2("val", "");
		$("#protocol").select2("val", "");
		$("#packetFormat").select2("val", "");
		$("#idnteractive").select2("val", "");
		$("#zcyh").select2("val", "请选择支持银行");
		$("#zfklx").select2("val", "请选择支持的支付卡类型");
		$("#zfksx").select2("val", "请选择支持的支付卡属性");
		if(inputPlaceholder()){
			$scope.sel.key=keyTip;
	    	$scope.sel.ip=ipTip;
	    	$scope.sel.port=portTip;
	    }
    }
    
    if(inputPlaceholder()){
    	$scope.sel.key=keyTip;
    	$scope.sel.ip=ipTip;
    	$scope.sel.port=portTip;
    }
    
});

app.factory('InterService', function ($http) {
	var list = function (param) {
        return $http.post('qz/inters/getByPage',param);
    };
    var del = function (id){
    	return $http.put('qz/inters/del/'+id);
    }
	return {
		list:function(param){
			return list(param);
		},
		del:function(id){
			return del(id);
		}
	}
});
